
$(function () {
   obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        params:{
            month:null,
            schoolId:null,
            key:'',
            area:'',
            city:'',
            mode:0
        },
        dgState:'close',
        dom:{
          searchDom:null
        },
        init:function () {

            this.dataGrid =  new xhDataGrid($('#list'), 'school_examine/getDgData',this.params);
            this.dataGrid.parseToolbar();
            this.params.month  = xhExtra.getValue($('#list_search_date'));
            this.dataGrid.init();
            this.funObj().dataGridEevent();//添加dataGrid动作监听

            this.dom.searchDom   = $('#bt_list_search');
            this.listener();
        },
        listener:function () {
            var $this = this;
            //搜索
            $this.dom.searchDom.click(function () {
                $this.funObj().search();
            });
            //添加
            $('#bt_list_add').click(function () {
                $this.funObj().generate();
            });
            //地区
            $('#list_search_area').combobox({
                onSelect:function (rec) {
                    $this.funObj().initCity(rec);
                }
            });

            //导出
            $('#bt_list_export').click(function () {
                $this.funObj().exportExcel();
            });

            //收起
            $('#bt_list_pack').click(function () {
               $this.funObj().pack();
            });
            //展开
            $('#bt_list_expand').click(function () {
                $this.funObj().expand();
            })

        },
        funObj:function () {
            var $this = this;
            return {
                search:function () {
                    $this.params.month  = xhExtra.getValue($('#list_search_date'));
                    $this.params.key    = xhExtra.getValue($('#list_search'));
                    $this.params.area   = xhExtra.getValue($('#list_search_area'));
                    $this.params.city   = xhExtra.getValue($('#list_search_city'));
                    $this.params.level  = xhExtra.getValue($('#list_search_level'));
                    $this.params.mode  = xhExtra.getValue($('#list_search_mode'));
                    $this.dataGrid.reload($this.params);
                },
                dataGridEevent:function () {
                    $this.dataGrid.onDblClickCell = function(rowIndex, field, value){
                        var rowData = $this.dataGrid.command('getRows')[rowIndex];
                        switch (field){
                            case 'operate': $this.funObj().doGetGrade(rowData);break;
                            case 'sMonth' : $this.funObj().openYearGrade(rowData);break;
                            case 'sGrade' : $this.funObj().openGradeDetail(rowData);break;
                            // case 'sSchoolName' : $this.funObj().openSchoolRun(rowData);break;
                        }
                    };


                    $this.dataGrid.command({
                        view: detailview,
                        detailFormatter:function(index,row){
                            var str='<div class="ddv" style="padding:5px 0;overflow: auto;background-color: white;" >';
                                str+='<div id="chart_'+row['iSchoolId']+'" style="width: 100%;height: 300px;"></div>';
                            str+='</div>';
                            return str;
                        },
                        onExpandRow: function(index,row){
                            var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                            var config  = {showLegends: false, showZoom:true, seriesFontSize: 15};
                            var source  = row['extra'];
                            if(source!=null){
                                (new xhChart).line("chart_"+row['iSchoolId'], source, config);
                            }else {
                                $("#chart_"+row['iSchoolId']).html('没有数据');
                            }
                            $this.dataGrid.command('fixDetailRowHeight',index);
                        }
                    });
                    $this.dataGrid.onLoadSuccess=function (data) {
                        if($this.dgState == 'open'){
                            $this.funObj().expand();
                        }else if ($this.dgState == 'close'){
                            $this.funObj().pack();
                        }

                    }

                    // $this.dataGrid.onDblClickRow = function (rowIndex,rowData) { //双击展开右侧面板
                    //         var p = $("#mainLayout").layout("panel", "east")[0].clientWidth;
                    //         if (p <= 0) {
                    //             $('#mainLayout').layout('expand','east');
                    //         }
                    //         var schoolId = rowData['iSchoolId'];
                    //         var month    = $(rowData['sMonth']).text();
                    //         $('#gradeDetail').attr('src', baseUrl+'school_examine/gradeDetail/'+schoolId+'/'+month);
                    // };

                },
                doGetGrade:function (rowData) {
                    if(hasPower){
                        $this.params.month    = xhExtra.getValue($('#list_search_date'));
                        $this.params.schoolId = rowData['iSchoolId'];
                        xhExtra.askChangeStatus('school_examine/getGrade',$this.params,$this.dataGrid,'确定要生成嘛?',function (res) {
                            if(res.code === 200){
                                window.top.swal(res.msg,'','success');
                                $this.dataGrid.reload();
                            }
                        });
                    }else{
                        window.top.xhTop.showError('没有权限','');
                    }

                },
                openYearGrade:function (rowData) {
                    if(rowData['sGrade'] == null){
                        return false;
                    }
                    var schoolId = rowData['iSchoolId'];
                    window.top.xhTop.showModal({
                        title:'历年成绩',
                        content:'<iframe name="targetFrame" src="'+baseUrl+'school_examine/yearGrade/'+schoolId+'"  style="width: 100%;;border-style: none;padding: 0px;height:600px"  scrolling="yes"   ></iframe>',
                        backClose:false
                    })
                },
                openGradeDetail:function (rowData) {
                    if(rowData['sGrade'] == null){
                        return false;
                    }
                    // var schoolId = rowData['iSchoolId'];
                    // var month    = $(rowData['sMonth']).text();
                    // window.top.xhTop.showModal({
                    //     title:'成绩详情',
                    //     content:'<iframe name="targetFrame" src="'+baseUrl+'school_examine/gradeDetail/'+schoolId+'/'+month+'"  style="width: 100%;;border-style: none;padding: 0px;height:600px"  scrolling="yes"   ></iframe>',
                    //     backClose:false,
                    // })
                    var p = $("#mainLayout").layout("panel", "east")[0].clientWidth;
                    if (p <= 0) {
                        $('#mainLayout').layout('expand','east');
                    }
                    var schoolId = rowData['iSchoolId'];
                    var month    = $(rowData['sMonth']).text();
                    $('#gradeDetail').attr('src', baseUrl+'school_examine/gradeDetail/'+schoolId+'/'+month);
                },
                generate:function () {
                    var schoolIds = $this.dataGrid.getSelectValues('iSchoolId');
                    $this.params.month  = xhExtra.getValue($('#list_search_date'));
                    xhExtra.askChangeStatus('school_examine/generate',{schoolIds:schoolIds,month:$this.params.month},$this.dataGrid,'确定要生成嘛?',function (res) {
                        if(res.code === 200){
                            window.top.swal(res.msg,'','success');
                            $this.dataGrid.reload();
                        }
                    });

                },
                initCity:function (rec) {
                    var pid = rec.value;
                    $('#list_search_city').combobox({
                        url:baseUrl+'school_examine/getDeps/'+pid,
                        valueField: 'id',
                        textField: 'text'
                    });
                },
                openSchoolRun:function (rowData) {
                    window.top.xhTop.createNewTab(rowData['sSchoolName']+'-考核状况',baseUrl+'school_examine/schoolRun?schoolId='+rowData['iSchoolId']);
                },
                exportExcel:function () {
                    $this.funObj().search();
                    window.open(baseUrl+'school_examine/exportExamine?month='+$this.params.month+'&area='+$this.params.area+'&city='+$this.params.city+'&key='+$this.params.key+'&level='+$this.params.level);
                },
                pack:function () {
                    $this.dgState = 'close';
                    var row = $this.dataGrid.command("getRows");
                    for (var r = 0; r < row.length; r++)
                    {
                        if(row[r]['extra'] != null){
                            $this.dataGrid.command("collapseRow",r);
                        }
                    }
                },
                expand:function () {
                    $this.dgState = 'open';
                    var row = $this.dataGrid.command("getRows");
                    for (var r = 0; r < row.length; r++)
                    {
                        if(row[r]['extra'] != null){
                            $this.dataGrid.command("expandRow",r);
                        }
                    }
                }
            }
        }
    }
};
